from OS import *
from Program import *
from CPUInstruction import *
from multiprocessing import Queue
from IOInstructionPrinter import *
from IOInstructionScreen import *
from IOInstructionKeyBoard import *  

import unittest
from ManagerCPU import CPUManager


class PriorityTest(unittest.TestCase):


    def setUp(self):

        self.outputQueue = Queue()

        self.scheduler = Priority()
        
        self.rm = ResourceManager()
        self.rm.registerResource("kb", 1, self.scheduler.readyQueue, self.outputQueue)
        self.rm.registerResource("screen", 1, self.scheduler.readyQueue, self.outputQueue)
        self.rm.registerResource("printer", 1, self.scheduler.readyQueue, self.outputQueue)
        self.cpuManager = CPUManager(1, self.rm,self.outputQueue,self.scheduler)

        self.myOS = OS(self.scheduler, self.rm, self.cpuManager,self.outputQueue)
        self.myProg = Program()
        self.myProg.setPriority(1)
        self.myProg2 = Program()
        self.myProg2.setPriority(2)
        self.in1=CPUInstruction(1,"cpu prog")
        self.in2=CPUInstruction(1,"cpu prog1")
        self.in3=CPUInstruction(1,"cpu prog2")
        self.in4=CPUInstruction(1,"cpu prog3")
        self.in5=CPUInstruction(1,"cpu prog4")
        
        
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in2)
        self.myProg2.addInstruction(self.in3)
        self.myProg2.addInstruction(self.in4)
        self.myProg2.addInstruction(self.in5)
        
        self.myOS.addProgram(self.myProg)
        self.myOS.addProgram(self.myProg2)
        self.myOS.addProgram(self.myProg)
        





    def testPriorityOutput(self): 
        self.myOS.startUP()
        time.sleep(15)       
        self.assertEqual(self.outputQueue.qsize(), 7)
        self.assertEqual(self.outputQueue.get(),("cpu prog",1))
        self.assertEqual(self.outputQueue.get(),("cpu prog1",1))
        self.assertEqual(self.outputQueue.get(),("cpu prog",1))
        self.assertEqual(self.outputQueue.get(),("cpu prog1",1))
        self.assertEqual(self.outputQueue.get(),("cpu prog2",1))
        self.assertEqual(self.outputQueue.get(),("cpu prog3",1))
        self.assertEqual(self.outputQueue.get(),("cpu prog4",1))
        
    
        
    
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    
suite = unittest.TestLoader().loadTestsFromTestCase(PriorityTest)
unittest.TextTestRunner(verbosity=2).run(suite)
